package cn.fanxin.tools;

import cn.fanxin.enums.ConstantEnum;

import java.util.Properties;

/**
 * @Author：china-fanxin
 * @Website: https://fan-xin.cn
 * @Date 2023/3/3 13:45
 * @Description: 生成公共的shell命令类
 */
public class CommonShell {

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 生成插入adb目标集群前
     * @Date 2023/3/3 13:56
     * @Param: [acctDate, kpiCode, tableName, configProp]
     * @Return: java.lang.String
     */
    public static String genAdbDeleteShell(String acctDate, String kpiCode, String tableName, Properties configProp){
        String sql = "";
        String command;
        String user = configProp.getProperty("adbUser");
        String password = configProp.getProperty("adbPassword");
        if (acctDate.length()==6){
            sql = "delete from " + tableName + "_MID where month_id = \"" + acctDate + "\" and kpi_code = \"" + kpiCode + "\";";
        }else if (acctDate.length()==8){
            sql = "delete from " + tableName + "_MID where date_id = \"" + acctDate + "\" and kpi_code = \"" + kpiCode + "\";";
        }
        command = "mysql -h10.177.159.51 -P3306 -u"+user+" -p"+password+" -DCUBE_GX -e '" + sql + "'";
        CommonTool.outputInfoTip("入库前执行的删除历史数据语句为\n" + "mysql -h10.177.159.51 -P3306 -u"+user+" -p******"+" -DCUBE_GX -e '" + sql + "'\n");
        return command;
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 生成入库Adb的执行语句（文件含有表头）
     * @Date 2023/3/3 14:37
     * @Param: [tableName]
     * @Return: java.lang.String
     */
    public static String genAdbImportShell(String tableName){
        String command;
        command = "/home/target/jf_scripts/shell/importADB.sh " + ConstantEnum.OUTPUT_FILE_PATH.getValue() + " " + tableName + "_MID";
        CommonTool.outputInfoTip("adb入库语句为：\n" + command + "\n");
        return command;
    }


    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 生成入库Adb的执行语句(文件不包含表头部分）
     * @Date 2023/3/9 15:48
     * @Param: [tableName]
     * @Return: java.lang.String
     */
    public static String genAdbImportKShell(String tableName){
        String command;
        command = "/home/target/jf_scripts/shell/importADB-k.sh " + ConstantEnum.OUTPUT_FILE_PATH.getValue() + " " + tableName + "_MID";
        CommonTool.outputInfoTip("adb入库语句为：\n" + command + "\n");
        return command;
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 根据adb的中间表自动生成MC上的建表语句
     * @Date 2023/3/9 14:38
     * @Param: [acctDate, tableName, kpiCode]
     * @Return: java.lang.String
     */
    public static String genAdbDdlSql(String acctDate,String tableName,String kpiCode){
        StringBuilder ddlSql = new StringBuilder();
        ddlSql.append("cat << EOF > ").append(ConstantEnum.GEN_DDL_SQL_PATH.getValue()).append("\n")
                .append("SELECT 'CREATE TABLE IF NOT EXISTS ").append(tableName).append("_").append(acctDate).append("_").append(kpiCode).append("_TEMP ('")
                .append("||group_concat(info)||')' \nFROM (\n\t")
                .append("SELECT column_name || ' ' || case when data_type = 'varchar' then 'STRING' \n\t\t")
                .append("WHEN DATA_TYPE = 'decimal' THEN 'decimal(36,18)' END || ' COMMENT \"' || column_comment || '\"' as info\n\t")
                .append("FROM information_schema.COLUMNS\n")
                .append("WHERE table_name = '").append(tableName).append("_MID'").append("AND extra != 'auto_increment'\n")
                .append("ORDER BY ORDINAL_POSITION\n)")
                .append("\nEOF");
        CommonTool.outputInfoTip("生成MC临时表建表语句为：\n"+ddlSql.toString());
        return ddlSql.toString();
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 查询多维结果表中间表的建表语句
     * @Date 2023/3/8 10:11
     * @Param: [tableName]
     * @Return: java.lang.String
     */
    public static String genAdbDdlShell(){
        String command;
        command = "mysql -h10.177.159.51 -P3306 -ucubeselect -pem7hK23EdsZF -DCUBE_GX -N < " +  ConstantEnum.GEN_DDL_SQL_PATH.getValue() + ">" + ConstantEnum.DDL_SQL_PATH.getValue();
        CommonTool.outputInfoTip("根据adb中间表生成mc临时表建表语句：\n"+
                "mysql -h10.177.159.51 -P3306 -ucubeselect -p****** -DCUBE_GX -N < " +  ConstantEnum.GEN_DDL_SQL_PATH.getValue() + ">" + ConstantEnum.DDL_SQL_PATH.getValue());
        return command;
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 删除过程中产生的临时SQL文件，避免时间久了占用大量空间
     * @Date 2023/3/3 15:12
     * @Param: []
     * @Return: java.lang.String
     */
    public static String genRmTmpExecSQLFile(){
        String command;
        command = "rm " + ConstantEnum.EXECUTE_SQL_PATH.getValue() + "\n";
        CommonTool.outputInfoTip("删除过程中的临时脚本，执行命令：\n" + command);
        return command;
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 删除过程中产生的临时SQL文件，避免时间久了占用大量空间
     * @Date 2023/3/3 15:12
     * @Param: []
     * @Return: java.lang.String
     */
    public static String genRmTmpDelSQLFile(){
        String command;
        command = "rm " + ConstantEnum.DELETE_SQL_PATH.getValue() + "\n";
        CommonTool.outputInfoTip("删除过程中的临时脚本，执行命令：\n" + command);
        return command;
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 删除过程中产生的临时输出文件，避免时间久了占用大量空间
     * @Date 2023/3/3 15:12
     * @Param: []
     * @Return: java.lang.String
     */
    public static String genRmTmpOutFile(){
        String command;
        command = "rm " + ConstantEnum.OUTPUT_FILE_PATH.getValue() + "\n";
        CommonTool.outputInfoTip("删除过程中的临时输出文件，执行命令：\n" + command);
        return command;
    }
}
